package com.yuanluludev.lib_x5web

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.tencent.smtt.sdk.QbSdk
import com.yuanluludev.lib_utils.util.LogUtils
import kotlinx.android.synthetic.main.activity_x5_web.*

/**
 * yuanLulu
 * 通用展示web页面(可用于展示web页面、也可以播放视频)
 */
class WebActivity : BaseWebViewActivity() {

    companion object {
        private const val WEB_URL = "web_url"
        private const val TITLE_TEXT = "title_text"

        @JvmStatic
        fun jump(context: Context, webUrl: String, titleText: String = "") {
            context.startActivity(Intent(context, WebActivity::class.java).apply {
                putExtra(WEB_URL, webUrl)
                putExtra(TITLE_TEXT, titleText)
            })
        }
    }

    override fun getLayoutResId() = R.layout.activity_x5_web

    override fun getContainerID() = R.id.rlContainer

    override fun initEvents() {
        val titleText = intent?.getStringExtra(TITLE_TEXT) ?: ""
        if (!TextUtils.isEmpty(titleText)) {
            tvTitle?.text = titleText
        }

        //回退事件
        ivGoBack?.setOnClickListener {
            if (mWebView != null && mWebView?.canGoBack() == true) {
                mWebView?.goBack()
                return@setOnClickListener
            }
            finish()
        }

        //如果是视频或者图片 隐藏退出按钮
        val webUrl = (intent?.getStringExtra(WEB_URL) ?: "")
        if(webUrl.endsWith("mp4")||webUrl.endsWith("jpg")||webUrl.endsWith("jpeg")||webUrl.endsWith("png")){
            ivBack?.visibility = View.GONE
        }

        LogUtils.d("X5WebInit", "--WebActivity--- x5 内核 ==  ${mWebView?.x5WebViewExtension}  如果为null  就没有加载成功")
        LogUtils.d("X5WebInit", "WebActivity  内核版本号 = ${QbSdk.getTbsVersion(this)}")
    }

    override fun setTitleText(title: String) {
        val titleText = intent?.getStringExtra(TITLE_TEXT) ?: ""
        if (TextUtils.isEmpty(titleText)) {
            tvTitle?.text = title
        }
    }

    override fun getWebUrl(): String {
        return intent?.getStringExtra(WEB_URL) ?: ""
    }

}